import { isArray, isObject } from '../share'
import { reactiveArray } from './rcArray'
import { defineProperty } from './rcObject'

/**
 * 设置响应式
 * @param {Object|Array} target
 */
export function reactive(target) {
  if (!isObject(target)) {
    return target
  }
  if (target.__ob__) return
  if (isArray(target)) {
    reactiveArray(target)
  } else {
    reactiveObject(target)
    setOb(target, true)
  }
}

export function reactiveObject(target) {
  Object.keys(target).forEach(key => {
    defineProperty(target, key, target[key])
  })
}

export function setOb(target, value) {
  Object.defineProperty(target, '__ob__', {
    value,
    configurable: false,
    writable: false,
    enumerable: false
  })
}
